package information;

import java.util.Calendar;

/**
 * @author Natalie
 * @author Christian (dateToNumber())
 *
 */
public class Date {
	
	private int myYear;
	
	private int myMonth;
	 
	private int myDay;
	
	private int myHour;
	
	private int myMinute;
	
	
	/**
	 * Creates a new date object. 
	 */
	//@requires all parameters != null
	//@ensures all fields are initialized. 
	public Date(final int theYear, final int theMonth, final int theDay, final int theHour, final int theMinute) {
		myYear = theYear;
		myMonth = theMonth;
		myDay = theDay;
		myHour = theHour;
		myMinute = theMinute;
	}
	
	/**
	 * Returns true if the job is in the future, false if it is not. 
	 */
	//@ requires myYear != null && myMonth != null && myDay != null
	public boolean isFuture() {
		boolean inFuture = true;
		Calendar cal = Calendar.getInstance();
		int day = cal.get(Calendar.DATE);
		int month = cal.get(Calendar.MONTH) + 1;
		int year = cal.get(Calendar.YEAR);
		if (year > myYear) inFuture = false;
		else if (year == myYear) {
			if (month > myMonth) inFuture = false;
			else if (month == myMonth) {
				if (day >= myDay) inFuture = false;
			}
		}
		return inFuture;
	}
	public boolean pastOr3Months() {
		Calendar cal = Calendar.getInstance();
		int day = cal.get(Calendar.DATE);
		int month = cal.get(Calendar.MONTH) + 1;
		int year = cal.get(Calendar.YEAR);
		int carry;
		if ( month > 9){
			carry = ( month - 9);
			if(year + 1 == myYear && myMonth > carry){
				return false;
			}
		}
		if (myMonth > 3 + month){
			return false;
		}
		
		if ( myYear < year ){
			return false;
		}	
		if ( myYear == year && myMonth < month ){
				return false;		
		}
		if ( myYear == year && myMonth == month && myDay < day){
			return false;
		}
		
		
		return true;
	}
	//assumes no volunteering on New years
	public boolean tooLong(final Date endDate) {
		int year = endDate.myYear;
		int month = endDate.myMonth;
		int day = endDate.myDay;
		if ( myYear == year && myMonth == month && myDay + 1 < day){	
			return false;
		}
		if ( myYear == year && myMonth + 1 == month && 1 < day){	
			return false;
		}	
		return true;		
	}
	//starts counting the day before MyDay 
	// counts all days after the 29th in case of "reverse month"
	public int JobsBefore(final Date endDate) {
		int year = endDate.myYear;
		int month = endDate.myMonth;
		int day = endDate.myDay;
		int jobcount = 0;
		
		if ( myYear == year && myMonth == month && myDay > 5 && myDay < 4 + day){
			jobcount++;
		}
		
		if ( myYear == year && myMonth == month && myDay <= 4 && day < 4){
			jobcount++;
		}
		
		if ( myYear == year && myMonth == month - 1 && myDay <= 4 && day > 24 && 24 + myDay > day){
			jobcount++;
		}
		return jobcount;	
	}
	// starts counting the day after MyDay 
	// counts all days after the 29th in case of "advance month"
	public int JobsAfter(final Date StartDate){
		int year = StartDate.myYear;
		int month = StartDate.myMonth;
		int day = StartDate.myDay;
		int jobcount = 0;
		if ( myYear == year && myMonth == month && myDay < 24 && myDay + 4 > day){
			jobcount++;
		}
		if ( myYear == year && myMonth == month && myDay >= 24 && day > 24 ){
			jobcount++;
		}
		if ( myYear == year && myMonth == month +1 && myDay >= 24 && day <= 5 && myDay - 24 >= day){
			jobcount++;
		}
		return jobcount;		
	}
	
	public int daysLong(final Date endDate) {
		return myDay;
	}
	
	public String toString() {
		return myMonth + "/" + myDay + "/" + myYear + "  " + myHour + ":" + myMinute;
	}
	
	public String toXmlString() {
		return myMonth + "/" + myDay + "/" + myYear + "/" + myHour + "/" + myMinute;
	}
	
	/**
	   * Returns true if the two objects are equal. Returns false
	   * if the two objects are not equal. Two dates are equal if they
	   * have the same year, month and day.
	   * 
	   * @param the_other An object to be compared. 
	   * @return True or False of whether the two objects are equal  
	   * @author Natalie
	   */
	//@requires theOther != null
	//@requires myYear != null && myMonth != null && myDay != null 
	public boolean equals(final Object theOther) {
		boolean isEqual = false;
		if (theOther == this ) isEqual = true;
		else if (theOther instanceof Date) {
			final Date theOtherCast = (Date) theOther;
			isEqual = myYear == theOtherCast.myYear && myMonth == theOtherCast.myMonth && myDay == theOtherCast.myDay;
		}
		return isEqual;
	}
	
	/**
	 * Converts a date d/m/yyyy to a number.
	 * @author Christian
	 * @return The number representation of this date
	 */
	//@requires myDay != null, myMonth != null, myYear != null
	public long dateToNumber() {
		return myDay+(myMonth*100)+(myYear*100);
	}
}
